<!-- <template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="工单号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.docNo`" :rules="formRules.docNo" class="mb-0px!">
            <el-input v-model="row.docNo" placeholder="请输入工单号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="物料号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itemCode`" :rules="formRules.itemCode" class="mb-0px!">
            <el-input v-model="row.itemCode" placeholder="请输入物料号" />
          </el-form-item>
        </template>
      </el-table-column>
       <el-table-column label="位置" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.location`" :rules="formRules.location" class="mb-0px!">
            <el-input v-model="row.location" placeholder="请输入位置" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="批次号" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.batchNo`" :rules="formRules.batchNo" class="mb-0px!">
            <el-input v-model="row.batchNo" placeholder="请输入批次号" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qty`" :rules="formRules.qty" class="mb-0px!">
            <el-input v-model="row.qty" placeholder="请输入数量" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="数据来源" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.dataSource`" :rules="formRules.dataSource" class="mb-0px!">
            <el-input v-model="row.dataSource" placeholder="请输入数据来源" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="类型(出库/入库)" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.stockType`" :rules="formRules.stockType" class="mb-0px!">
            <el-select v-model="row.stockType" placeholder="请选择类型(出库/入库)">
                <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加物料出入库记录</el-button>
  </el-row>
</template> -->
<script setup lang="ts">
import { MaterialInventorApi } from '@/api/opticore/materialinventor'

const props = defineProps<{
  serialNo: undefined // 序列号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.serialNo,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await MaterialInventorApi.getMaterialStockListBySerialNo(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    docNo: undefined,
    itemCode: undefined,
    serialNo: undefined,
    location: undefined,
    batchNo: undefined,
    qty: undefined,
    dataSource: undefined,
    stockType: undefined
  }
  row.serialNo = props.serialNo
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>